package com.jqbd.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jqbd.common.R;
import com.jqbd.dto.AdminDto;
import com.jqbd.pojo.Admin;
import com.jqbd.pojo.Education;
import com.jqbd.service.AdminService;
import com.jqbd.util.JsonResult;
import com.jqbd.util.KaptchaUtil;
import com.jqbd.util.OssUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author jh
 * Created by Enzo Cotter on 2022/11/11.
 */
@RestController
@RequestMapping("admin")
public class AdminController {

    @Resource
    public AdminService adminService;


    /**
     * 查询所有管理员信息
     * @return
     */
    @RequestMapping("findAll")
    public List<Admin> findAll() {
        return adminService.findAll();
    }
    /**
     * 分页查询管理员信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("findByPage")
    public JsonResult<Admin> findByPage(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "limit",required = false,defaultValue = "3") Integer pageSize

    ){
        PageHelper.startPage(pageNum,pageSize);

        List<Admin> musicList = adminService.findAll();

        PageInfo<Admin> patientPageInfo = new PageInfo<>(musicList);

        JsonResult jsonResult = JsonResult.ok();

        jsonResult.setCount(patientPageInfo.getTotal());

        jsonResult.setData(patientPageInfo.getList());

        return jsonResult;

    }

    /**
     * 添加管理员信息
     *
     */
    @RequestMapping("add")
    public R add( Admin admin) throws IOException {

        System.out.println(admin);

        int add = adminService.add(admin);

        return add>1? R.ok():R.error();
    }

    /**+   
     * 修改管理员信息
     * @param admin 管理员对象
     * @return
     */
    @RequestMapping("update")
    public String update(Admin admin) {

        adminService.update(admin);

        return "success";
    }

    /**
     * 删除单个管理员信息
     * @param adminId 学历编号
     * @return
     */
    @RequestMapping("deleteById")
    public R deleteById(Integer adminId) {

        int i = adminService.deleteById(adminId);

        return i>0?R.ok():R.error();
    }

    /**
     * 批量删除管理员信息
     * @return
     */
    @RequestMapping("deleteByIds")
    public R deleteByIds(Integer[] ids) {
        System.out.println(ids);
        for (Integer id : ids) {
            System.out.println(id);
        }
        int i = adminService.deleteByIds(ids);

        return i>0?R.ok():R.error();
    }

    /**
     * 登陆
     * @param adminDto
     * @param session
     * @return
     */
    @RequestMapping("login")
    public JsonResult<Admin> login(AdminDto adminDto,HttpSession session){
        //判断

        JsonResult jsonResult= JsonResult.ok();
        if (session.getAttribute("code").toString().equals(adminDto.getCaptcha())){
            //通过用户名和密码去登录
            String username = adminDto.getUsername();
            String password = adminDto.getPassword();
            //调佣
            Admin db_admin =   adminService.login(username,password);
            jsonResult.setData(db_admin);
            return jsonResult;
        }

        return JsonResult.fail();
    }

    /**
     * 退出
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //把登录信息从session中删除，跳转到登录页面
        //获取session
        //false表示获取session，如果没有就是null，
        //true默认不写，有session获取session，没有session创建session
        HttpSession session = request.getSession(false);


        //移除数据
        //session.removeAttribute("admin");
        //销毁session
        session.invalidate();

        //跳转页面
        response.sendRedirect(request.getContextPath() + "/login.html");

    }


    /**
     * 生成验证码
     * @param session
     * @param response
     * @throws IOException
     */
    @RequestMapping("createCaptcha")
    public void createCaptcha(HttpSession session, HttpServletResponse response) throws IOException {

        KaptchaUtil.createKaptchaImg(session,response);

        System.out.println("验证码结果：" + session.getAttribute("code").toString());

    }

    @RequestMapping("/load")
    public R load(MultipartFile file) throws IOException {
        String picUrl = OssUtil.uploadFile(file.getInputStream(), file.getOriginalFilename());
        return picUrl != null ? R.ok().data("picUrl",picUrl) : R.error().data("url","图片找不到了,请重新上传");
    }


    @RequestMapping("writeExcel")
    public R writeExcel(Integer [] ids, HttpServletResponse response){
        //System.out.println(Arrays.toString(ids));
        adminService.writeExcel(ids,response);
        return R.ok();
    }





}
